package com.example.casper.Experiment2024.data;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import androidx.test.platform.app.InstrumentationRegistry;

import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.Test.ImageUtils;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBankTest {
    private Context appContext;
    private DataBank dataBankBackup;
    private final String tempFileName = "temp_Records.data";

    @Before
    public void setUp() {
        appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        dataBankBackup=new DataBank(appContext);
        List<Records> originalRecords = new ArrayList<>(dataBankBackup.readRecords());
        copyRecordsToFile(originalRecords);
    }

    @After
    public void tearDown() {
        List<Records> tempRecords = readRecordsFromFile();
        dataBankBackup.saveRecords(tempRecords);
        appContext.deleteFile(tempFileName);
    }

    @Test
    public void saveandreadBooks() {
        ArrayList<String> photo1=new ArrayList<>();
        photo1.add(String.valueOf(R.drawable.funny_1));
        photo1.add(String.valueOf(R.drawable.funny_2));
        ArrayList<String> photo2=new ArrayList<>();
        photo2.add(String.valueOf(R.drawable.funny_3));
        photo2.add(String.valueOf(R.drawable.funny_4));
        ArrayList<String> photo3=new ArrayList<>();
        photo3.add(String.valueOf(R.drawable.funny_5));
        photo3.add(String.valueOf(R.drawable.funny_6));
        List<Records> RecordList=new ArrayList<>();
        RecordList.add(new Records("暨南大学","暨南大学（Jinan University）简称“暨大”，本部位于广东省广州市，学校是中央统战部、教育部、广东省人民政府共建的国家“双一流”建设高校，直属中央统战部管理；位列国家“双一流”、“211工程”、 “985工程优势学科创新平台” 、广东省高水平大学重点建设高校。"
                ,22.25198333333333,113.53601,photo1,"广东省珠海市香洲区前山路206号暨南大学(珠海校区)"));
        RecordList.add(new Records("圆明新园","圆明新园（New Yuanming Palace），位于广东省珠海市香洲区兰埔路与白石路交叉路口西侧。 1992年10月18日，圆明新园奠基，由清华大学建筑学院资深教授设计，以北京圆明园被焚烧前建筑为原稿，根据特定的地理环境、在不破坏原有生态环境的前提下，按1:1比例选建圆明园四十景中的十八景。1997年春节，圆明新园正式对外开放。"
                ,22.242564,113.539333,photo2,"广东省珠海市香洲区兰埔路与白石路交界处圆明新园"));
        RecordList.add(new Records("富华里","珠海拱北新地标：富华里\n" +
                "珠海拱北又添新去处！富华里时尚天街，与香港风情相似，给你不一样的体验。\n" +
                "位于香洲区粤海中路与港一路交汇，周边办公楼、居民区和学校众多，消费人群超65万。\n" +
                "总建面5万㎡，集酒店、公寓、写字楼、商业于一体，打造地标商业旗舰。"
                ,22.2345,113.5413,photo3,"广东省珠海市香洲区拱北九洲大道西与白石路交汇处拱北富华里商业街区(象街)"));
        DataBank dataBank=new DataBank(appContext);
        dataBank.saveRecords(RecordList);
        List<Records> readRecords =dataBank.readRecords();
        Assert.assertNotSame(RecordList,readRecords);
        for (int i = 0; i < RecordList.size(); i++) {
            Assert.assertNotSame(RecordList.get(i),readRecords.get(i));
            Assert.assertEquals(RecordList.get(i).getTitle(),readRecords.get(i).getTitle());
            Assert.assertEquals(RecordList.get(i).getDescription(),readRecords.get(i).getDescription());
            Assert.assertEquals(RecordList.get(i).getLatitude(),readRecords.get(i).getLatitude(),0);
            Assert.assertEquals(RecordList.get(i).getLongitude(),readRecords.get(i).getLongitude(),0);
            Assert.assertEquals(RecordList.get(i).getLocationname(),readRecords.get(i).getLocationname());
            int length=RecordList.get(i).getPhoto().size();
            for (int j = 0; j < length; j++) {
                Assert.assertTrue(ImageUtils.areImagesSame(getCoverBitmap(Integer.parseInt(RecordList.get(i).getPhoto().get(j))), getCoverBitmap(Integer.parseInt(readRecords.get(i).getPhoto().get(j)))));
            }
        }
    }
    public Bitmap getCoverBitmap(int coverResourceId) {
        Resources res = appContext.getResources();
        return BitmapFactory.decodeResource(res, coverResourceId);
    }
    private void copyRecordsToFile(List<Records> recordsList) {
        FileOutputStream fileout;
        try {
            fileout = appContext.openFileOutput(tempFileName, Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fileout);
            out.writeObject(recordsList);
            out.close();
            fileout.close();
        } catch (IOException ignored) {
        }
    }

    private List<Records> readRecordsFromFile() {
        List<Records> records = new ArrayList<>();
        try {
            FileInputStream fileIn = appContext.openFileInput(tempFileName);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            records = (List<Records>) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException | ClassNotFoundException ignored) {
        }
        return records;
    }

}